package com.example.tabbar.fragment

import android.annotation.SuppressLint
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.lifecycle.ViewModelProvider
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import com.example.tabbar.adapter.RoomDataAdapter
import com.example.tabbar.databinding.FragmentRoomDataDetailsBinding
import com.junray.usermangement.viewModel.MeasurementViewModel


class RoomDataDetailsFragment : Fragment() {
    private lateinit var binding: FragmentRoomDataDetailsBinding
    private var userId: Int = -1
    private var locationId: Int = -1
    private var roomName = ""
    private var meaViewModel : MeasurementViewModel? = null
    private var recyclerView: RecyclerView? = null
    private var roomDataAdapter: RoomDataAdapter? = null

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        arguments?.let {
            userId = it.getInt(ARG_USER_ID, -1)
            locationId = it.getInt(ARG_LOCATION_ID, -1)
            roomName = it.getString(ROOM_NAME, "")
        }
    }

    override fun onCreateView(
        inflater: LayoutInflater, container: ViewGroup?,
        savedInstanceState: Bundle?
    ): View {
        binding = FragmentRoomDataDetailsBinding.inflate(inflater, container, false)
        return binding.root
    }

    override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
        super.onViewCreated(view, savedInstanceState)

        meaViewModel = ViewModelProvider(this)[(MeasurementViewModel::class.java)]
        initView()
        binding.roomDataBack.setOnClickListener {
            // fragment返回
            (parentFragment as? SearchFragment)?.backToSamplingList()
        }



    }
    private fun initView(){
        recyclerView = binding.samplingList
        roomDataAdapter = RoomDataAdapter()
        recyclerView?.adapter = roomDataAdapter
        recyclerView?.layoutManager = LinearLayoutManager(requireContext())

        if (roomName != "") {
            binding.roomName.text = roomName
        }

        if(userId != -1 && locationId != -1){
            meaViewModel!!.getRoomMeasurementData(
                userId,
                locationId
            ).observe(viewLifecycleOwner) { measurements ->
                if (measurements.isNotEmpty()) {
                    roomDataAdapter?.setItems(measurements)
                }
            }
        }
    }


    @SuppressLint("StaticFieldLeak")
    companion object {
        private const val ARG_USER_ID = "user_id"
        private const val ARG_LOCATION_ID = "location_id"
        private const val ROOM_NAME = "room_name"



        @JvmStatic
        fun newInstance(userId: Int? = null, locationId: Int? = null, roomName: String? = ""): RoomDataDetailsFragment =

             RoomDataDetailsFragment().apply {
                arguments = Bundle().apply {
                    putInt(ARG_USER_ID, userId ?: -1)
                    putInt(ARG_LOCATION_ID, locationId ?: -1)
                    putString(ROOM_NAME, roomName)
                }
            }
        }
}